package sessions;

import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entities.Amicizia;
import entities.RichiestaAmicizia;
import entities.Utente;

@Stateless
public class AmiciziaManager implements AmiciziaManagerRemote, AmiciziaManagerLocal {
	@PersistenceContext(unitName = "SwimDB")
	EntityManager database;

	@Override
	public boolean aggiungiAmicizia(String mittente, String destinatario) {
	
		Hashtable<String,String> env = new Hashtable<String,String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL,"localhost:1099");
		InitialContext jndiContext;
		try {
			jndiContext = new InitialContext(env);
		
		jndiContext = new InitialContext(env);

		Object ref = jndiContext.lookup("UserManager/local");
		UserManagerLocal user = (UserManagerLocal) ref;
		
		Object ref2 = jndiContext.lookup("RichiestaAmiciziaManager/local");
		RichiestaAmiciziaManagerLocal richiesta= (RichiestaAmiciziaManagerLocal) ref2;
		
		Utente mit=user.getUserWithUsername(mittente);
		Utente dest=user.getUserWithUsername(destinatario);
		
		if(richiesta.sonoGiaAmici(mit, dest))
			return false;
		
		RichiestaAmicizia r=richiesta.getRichiestaAmicizia(mit, dest);
		r.setResponso("accettata");
			
		
		Date data=new Date();
		try {
			Amicizia amicizia=new Amicizia();
			amicizia.setData(data);
			amicizia.setDestinatario(dest);
			amicizia.setMittente(mit);

			database.persist(amicizia);
			return true;
		}

		catch (EntityNotFoundException exc) { 
		} catch (NonUniqueResultException exc) {
			return false;
		}
		
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return false;
		
		
		
	}

	@Override
	public List<Amicizia> getAmicizie(String userName) {
		List<Amicizia> amici=new ArrayList<Amicizia>();
		Hashtable<String,String> env = new Hashtable<String,String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL,"localhost:1099");
		InitialContext jndiContext;
		try {
			jndiContext = new InitialContext(env);
		
		jndiContext = new InitialContext(env);

		Object ref = jndiContext.lookup("UserManager/local");
		UserManagerLocal user = (UserManagerLocal) ref;
		
		Utente utente=user.getUserWithUsername(userName);
		
		
		
		Query q = database.createQuery("FROM Amicizia a WHERE a.mittente=?1 OR a.destinatario=?2");
		q.setParameter(1, utente);
		q.setParameter(2, utente);
		List<?> risultato =  q.getResultList();
		
		for(Object obj: risultato) {
			amici.add((Amicizia) obj);
		}
		
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return amici;
		
		
	}


	@Override
	public List<Utente> getAmici(String userName) {

		List<Utente> amici=new ArrayList<Utente>();
		Hashtable<String,String> env = new Hashtable<String,String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL,"localhost:1099");
		InitialContext jndiContext;

		try {
			jndiContext = new InitialContext(env);

			Object ref = jndiContext.lookup("UserManager/local");
			UserManagerLocal u= (UserManagerLocal) ref;


			Utente utente=u.getUserWithUsername(userName);



			Query q = database.createQuery("FROM Amicizia a WHERE a.mittente=?1 OR a.destinatario=?2");
			q.setParameter(1, utente);
			q.setParameter(2, utente);
			List<?> risultato =  q.getResultList();

			for(Object obj: risultato) {
				if(((Amicizia)obj).getMittente().getUserName().compareTo(userName)==0)
				{
					amici.add(((Amicizia) obj).getDestinatario());
				}
				else
				{
					amici.add(((Amicizia)obj).getMittente());
				}
			}



		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		return amici;

	}
	
	
	
	
	
	
}
